package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.constant.PageResult;
import com.itheima.constant.QueryPageBean;
import com.itheima.constant.Result;
import com.itheima.domain.Checkgroup;
import com.itheima.service.CheckGroupService;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author Zhang_Han
 * @version 1.0
 * @date 2020/9/25 10:19
 */
@RestController
@RequestMapping("/checkgroup")
public class CheckGroupController {

    @Reference
    private CheckGroupService checkGroupService;


    /*添加数据*/
    @RequestMapping("/add")
    public Result add(@RequestBody Checkgroup checkgroup, Integer[] checkitemIds) {
        try {
            checkGroupService.add(checkgroup, checkitemIds);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.ADD_CHECKGROUP_FAIL);
        }
        return new Result(true, MessageConstant.ADD_CHECKGROUP_SUCCESS);
    }

    /*分页条件查询*/
    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean) {
        PageResult PageResult = checkGroupService.findPage(queryPageBean);
        return PageResult;
    }

    /*删除数据*/
    @RequestMapping("/deleteByid")
    public Result deleteByid(Integer id) {
        try {
            checkGroupService.deleteByid(id);
            return new Result(true, MessageConstant.DELETE_CHECKGROUP_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.DELETE_CHECKGROUP_FAIL);
        }
    }

    /*根据id查询用于编辑的数据回显*/
    @RequestMapping("/findById")
    public Result findById(Integer id) {
        try {
            Checkgroup checkgroup = checkGroupService.findById(id);
            return new Result(true, MessageConstant.QUERY_CHECKGROUP_SUCCESS, checkgroup);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_CHECKGROUP_FAIL);
        }
    }

    /*根据检查组的id查询检查项的id,将其封装到一个list集合中返回给前端页面*/
    @RequestMapping("/getChencItemIDs")
    public Result getChencItemIDs(Integer id) {
        try {
            List<Integer> list = checkGroupService.getChencItemIDs(id);
            return new Result(true, MessageConstant.QUERY_CHECKITEM_SUCCESS, list);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_CHECKITEM_FAIL);
        }
    }

    /*编辑数据*/
    @RequestMapping("/update")
    public Result update(@RequestBody Checkgroup checkgroup, Integer[] checkitemIds) {
        try {
            checkGroupService.update(checkgroup, checkitemIds);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.EDIT_CHECKGROUP_FAIL);
        }
        return new Result(true, MessageConstant.EDIT_CHECKGROUP_SUCCESS);
    }


    /*查询全部数据用于套餐维护的新增页面检查组数据回显*/
    @RequestMapping("/findAll")
    public Result findAll() {
        try {
            List<Checkgroup> list = checkGroupService.findAll();
            return new Result(true, MessageConstant.QUERY_CHECKGROUP_SUCCESS, list);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_CHECKGROUP_FAIL);
        }
    }
}
